package lcr;

public class Test0022 {
}

class Solution0022_1 {
    public ListNode detectCycle(ListNode head) {
        ListNode l=head,r=head;
        while (r!=null&&r.next!=null) {
            l=l.next;
            r=r.next.next;

            if (l==r) break;
        }
        if (r==null||r.next==null) return null;

        l=head;
        while (l!=r){
            l=l.next;
            r=r.next;
        }
        return l;
    }
}
